package com.dongchedi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongchedi.entity.CustomerInfo;
import com.dongchedi.entity.CustomerLogin;
import com.dongchedi.service.CustomerInfoService;
import com.dongchedi.service.impl.CustomerInfoServiceImpl;
import com.dongchedi.utils.HostHolder;
import com.dongchedi.utils.constant.RedisConstantKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author leibidong
 * @description   用户个人详情controller层
 * @date 2021/6/24
 */
@Controller
@RequestMapping("api")
@ResponseBody
public class CustomerInfoController {
    @Autowired
    CustomerInfoService customerInfoService;
    @Resource
    RedisTemplate<String,Object> redisTemplate;
    @Resource
    HostHolder hostHolder;
    /*
     * @Author: leibidong
     * @Description: 根据用户id查询用户个人信息
     * @Date: 2021/6/26 20:13
     * @Param: [customerId]
     * @Return: java.lang.String
     **/
    @GetMapping("/select/user/info")
    public String userInfo(Integer customerId){
        CustomerLogin customerLogin=hostHolder.getUser();
        if(customerId.equals(customerLogin.getId())) {
            String key = RedisConstantKey.getPersonInfo(customerId);
            if (redisTemplate.opsForValue().get(key) != null) {
                JSONObject json = new JSONObject();
                json.put("code", 1);
                json.put("msg", "查询成功");
                json.put("data",redisTemplate.opsForValue().get(key));
                return json.toJSONString();
            } else {
                CustomerInfo customerInfo = customerInfoService.queryByCustomerId(customerId);
                JSONObject json = new JSONObject();
                if (customerInfo != null) {
                    json.put("code", 1);
                    json.put("msg", "查询成功");
                    json.put("data", customerInfo);
                    redisTemplate.opsForValue().set(key, customerInfo, 3600, TimeUnit.SECONDS);
                } else {
                    json.put("code", 0);
                    json.put("msg", "查询失败");
                    json.put("data", null);
                }
                return json.toJSONString();
            }
        }else {
            JSONObject json = new JSONObject();
                json.put("code", 0);
                json.put("msg", "查询失败");
                json.put("data",null);
            return json.toJSONString();
        }
    }
    /*
     * @Author: leibidong
     * @Description: 更新个人信息
     * @Date: 2021/6/26 20:14
     * @Param: [customerInfo]
     * @Return: java.lang.String
     **/
    @PostMapping("/update/user/info")
    public  String  updateUserInfo(CustomerInfo customerInfo) {
        JSONObject json = new JSONObject();
        CustomerLogin customerLogin = hostHolder.getUser();
        if (customerInfo.getCustomerId().equals(customerLogin.getId())) {
            if (customerInfo.getCustomerId() != null) {
                if (customerInfo.getCustomerDesc() != null) {
                    return customerInfoService.updateDesc(customerInfo.getCustomerId(), customerInfo.getCustomerDesc());
                } else if (customerInfo.getCustomerPicUrl() != null) {
                    return customerInfoService.updatePic(customerInfo.getCustomerId(), customerInfo.getCustomerPicUrl());
                } else if (customerInfo.getCustomerName() != null) {
                    return customerInfoService.updateName(customerInfo.getCustomerId(), customerInfo.getCustomerName());
                }
            } else {
                json.put("code", 0);
                json.put("msg", "用户id为空");
                json.put("data", null);
                return json.toJSONString();
            }
            return null;
        }else {
            json.put("code", 0);
            json.put("msg", "用户id错误");
            json.put("data", null);
            return json.toJSONString();
        }
    }
}
